package com.hk.migration.config.mybatis;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import tk.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
public class MybatisConfiguration {

    @Configuration
    @MapperScan(basePackages = {"com.hk.migration.mapper.hk"},
            sqlSessionFactoryRef = "hkSqlSessionFactory")
    public static class MasterMybatisConfig {
        private final DataSource dataSource;

        public MasterMybatisConfig(@Qualifier("hkDataSource") DataSource dataSource) {
            this.dataSource = dataSource;
        }

        //创建SqlSessionFactory
        @Bean
        public SqlSessionFactory hkSqlSessionFactory() throws Exception {
            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource);
            factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath*:/mapper/hk/*.xml"));
            return factoryBean.getObject();
        }

        //利用SqlSessionFactory创建SqlSessionTemplate
        @Bean
        public SqlSessionTemplate hkSqlSessionTemplate() throws Exception {
            return new SqlSessionTemplate(hkSqlSessionFactory());
        }

        //事务
        @Bean
        public PlatformTransactionManager hkTransactionManager() {
            return new DataSourceTransactionManager(dataSource);
        }
    }


    @Configuration
    @MapperScan(basePackages = {"com.hk.migration.mapper.craftsman"},
            sqlSessionFactoryRef = "craftsmanSqlSessionFactory")
    public static class CraftsmanMybatisConfig {
        private final DataSource dataSource;

        public CraftsmanMybatisConfig(@Qualifier("craftsmanDataSource") DataSource dataSource) {
            this.dataSource = dataSource;
        }

        //创建SqlSessionFactory
        @Bean
        public SqlSessionFactory craftsmanSqlSessionFactory() throws Exception {
            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource);
            factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath*:/mapper/craftsman/*.xml"));
            return factoryBean.getObject();
        }

        //利用SqlSessionFactory创建SqlSessionTemplate
        @Bean
        public SqlSessionTemplate craftsmanSqlSessionTemplate() throws Exception {
            return new SqlSessionTemplate(craftsmanSqlSessionFactory());
        }

        //事务
        @Bean
        public PlatformTransactionManager craftsmanTransactionManager() {
            return new DataSourceTransactionManager(dataSource);
        }
    }


}
